/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.epsi.blokus.tools;

import java.io.FileWriter;
import java.io.IOException;

/**
 * Enregistrement des messages de Log
 * @author julien.lafont
 */
public class Log {

    public static enum LOG_OUTPUT { SCREEN, FILE };
    private static FileWriter fileWritter = null;

    // CONFIGURATION : Sortie à utiliser
    private static LOG_OUTPUT output = LOG_OUTPUT.SCREEN;

   
    /**
     * Log un message
     * @param str
     */
    public static void out(String str)
    {
        switch(output)
        {
            case SCREEN:
            default:
                log_screen(str);
            break;
            
            case FILE:
                log_file(str);
            break;
        }
    }

    public static void log_file(String str)
    {
        if (fileWritter == null) try {
            fileWritter = new FileWriter("log.txt", true);
            fileWritter.write(str);
        } catch (IOException ex) {
            log_screen(ex.getMessage());
        }
    }


    public static void log_screen(String str)
    {
        System.out.println(str);
    }

    /**
     * Ferme le fichier si nécessaire
     * @throws Throwable
     */
    @Override
    protected void finalize() throws Throwable {
        super.finalize();

        if (fileWritter!=null) fileWritter.close();
    }


}
